field types
pydantic 尽可能的使用标准库类型(standard library types)来标注字段来提供一个平滑的学习曲线;不过它也实现了许多常用的类型(commonly used types)
你当然可以创造属于自己的类型(your own pydantic-compatible types)
标准库类型
-
None,type(None)orLiteral[None]- 只允许
None值
- 只允许
-
bool- 详见
Booleans
- 详见
-
int- pydantic 使用
int(v)去将 v 强制转换成int
- pydantic 使用
-
float- 同样地,
float(v)也会将 v 强制转换成float
- 同样地,
-
strstr原样接收intfloat和Decimal会被str(v)强制转换bytes和bytearray会用v.decode()强制转换- 继承自
str的enum会被v.value强制转换 - 其他的类型都会报错
-
bytesbytes原样接收bytearray用bytes(v)转换str用v.encode()转换int,float和Decimal用str(v).encode()强制转换
-
list允许
list,tuple,set,frozenset,deque, 或者生成器(generators)并且将其转换为 list -
tupleallows
list,tuple,set,frozenset,deque, or generators and casts to a tuple; seetyping.Tuplebelow for sub-type constraints -
dictdict(v)is used to attempt to convert a dictionary; seetyping.Dictbelow for sub-type constraints -
setallows
list,tuple,set,frozenset,deque, or generators and casts to a set; seetyping.Setbelow for sub-type constraints -
frozensetallows
list,tuple,set,frozenset,deque, or generators and casts to a frozen set; seetyping.FrozenSetbelow for sub-type constraints -
dequeallows
list,tuple,set,frozenset,deque, or generators and casts to a deque; seetyping.Dequebelow for sub-type constraints -
datetime.datesee Datetime Types below for more detail on parsing and validation
-
datetime.timesee Datetime Types below for more detail on parsing and validation
-
datetime.datetimesee Datetime Types below for more detail on parsing and validation
-
datetime.timedeltasee Datetime Types below for more detail on parsing and validation
-
typing.Anyallows any value including
None, thus anAnyfield is optional -
typing.Annotatedallows wrapping another type with arbitrary metadata, as per PEP-593. The
Annotatedhint may contain a single call to theFieldfunction, but otherwise the additional metadata is ignored and the root type is used. -
typing.TypeVarconstrains the values allowed based on
constraintsorbound, see TypeVar -
typing.Unionsee Unions below for more detail on parsing and validation
-
typing.OptionalOptional[x]is simply short hand forUnion[x, None]; see Unions below for more detail on parsing and validation and Required Fields for details about required fields that can receiveNoneas a value. -
typing.List -
typing.Tuple -
subclass of typing.NamedTupleSame as
tuplebut instantiates with the given namedtuple and validates fields since they are annotated. See Annotated Types below for more detail on parsing and validation -
subclass of collections.namedtupleSame as
subclass of typing.NamedTuplebut all fields will have typeAnysince they are not annotated -
typing.Dictsee Typing Iterables below for more detail on parsing and validation
-
subclass of typing.TypedDictSame as
dictbut pydantic will validate the dictionary since keys are annotated. See Annotated Types below for more detail on parsing and validation -
typing.Setsee Typing Iterables below for more detail on parsing and validation
-
typing.FrozenSetsee Typing Iterables below for more detail on parsing and validation
-
typing.Dequesee Typing Iterables below for more detail on parsing and validation
-
typing.Sequencesee Typing Iterables below for more detail on parsing and validation
-
typing.Iterablethis is reserved for iterables that shouldn't be consumed. See Infinite Generators below for more detail on parsing and validation
-
typing.Typesee Type below for more detail on parsing and validation
-
typing.Callablesee Callable below for more detail on parsing and validation
-
typing.Patternwill cause the input value to be passed to
re.compile(v)to create a regex pattern -
ipaddress.IPv4Addresssimply uses the type itself for validation by passing the value to
IPv4Address(v); see Pydantic Types for other custom IP address types -
ipaddress.IPv4Interfacesimply uses the type itself for validation by passing the value to
IPv4Address(v); see Pydantic Types for other custom IP address types -
ipaddress.IPv4Networksimply uses the type itself for validation by passing the value to
IPv4Network(v); see Pydantic Types for other custom IP address types -
ipaddress.IPv6Addresssimply uses the type itself for validation by passing the value to
IPv6Address(v); see Pydantic Types for other custom IP address types -
ipaddress.IPv6Interfacesimply uses the type itself for validation by passing the value to
IPv6Interface(v); see Pydantic Types for other custom IP address types -
ipaddress.IPv6Networksimply uses the type itself for validation by passing the value to
IPv6Network(v); see Pydantic Types for other custom IP address types -
enum.Enumchecks that the value is a valid Enum instance
-
subclass of enum.Enumchecks that the value is a valid member of the enum; see Enums and Choices for more details
-
enum.IntEnumchecks that the value is a valid IntEnum instance
-
subclass of enum.IntEnumchecks that the value is a valid member of the integer enum; see Enums and Choices for more details
-
decimal.Decimalpydantic attempts to convert the value to a string, then passes the string to
Decimal(v) -
pathlib.Pathsimply uses the type itself for validation by passing the value to
Path(v); see Pydantic Types for other more strict path types -
uuid.UUIDstrings and bytes (converted to strings) are passed to
UUID(v), with a fallback toUUID(bytes=v)forbytesandbytearray; see Pydantic Types for other stricter UUID types -
ByteSizeconverts a bytes string with units to bytes